package cn.minds.capture.dispatch.server;

import cn.minds.capture.dispatch.task.Task;
import cn.minds.capture.dispatch.task.TaskHandler;

public class Worker extends Thread {

	private WorkerManager workerManager;

	public Worker(WorkerManager workerManager, int index) {
		this.workerManager = workerManager;
		setName("capture-worker-" + index);
	}

	@Override
	public void run() {
		workerManager.waitNotify();
		while (!workerManager.isStop()) {
			Task task = workerManager.distributeTask();
			try {
				if (task != null) {
					TaskHandler taskHandler = workerManager.createTaskHandler();
					taskHandler.doTask(task);
				} else {
					Thread.sleep(10000);
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

}
